<?php
//留言标签管理模块
class MimomentsTagAction extends Action {
	public function _initialize(){

		$this->retVal = Session::init();
        if (!$this->retVal) {
            Output::instance()->resultCode(code_access_denied);
            exit;
        }
		$cid = Session::$user["customerId"];
        Session::switchCustomer(0);
		$this->mimoment_tag_db = D('MimomentsTag');
	
		$this->cid = $cid;
		Session::switchCustomer($cid);
	}
		//得到所有标签
		public function getTagList($tagName=null){
			
			
		   $result = array();
			$condition='';
			if($tagName!=''){
				$condition['tagName'] = array("like","%".$tagName."%");
			}
			$arr  = $this->mimoment_tag_db->where($condition)->select();
			if ($arr) {
				$count = $this->mimoment_tag_db->count();
				$result['code'] = 1;
				$result['list'] = $arr;
				$result['count'] = $count;
				
			} else {
				$result['code'] = 0;
				$result['info'] = '暂无数据';
			}		
			 Output::instance()->object($result);
		
		}	
		
		
		//添加标签
		public function addTag($tag){
			
			    $result = array();		
				$where['tagName']=$tag;
				//判断标签表是否存在标签 若不存在则新建标签
				$res=$this->mimoment_tag_db->where($where)->find();	
				$accesstagid=$res['id'];
				if(!isset($res)){
					$return=$this->mimoment_tag_db->add($where);	
				//	echo $accessTagModel->getLastSql();	
				}
				
				
			if ($return) {
				$result['code'] = 1;
			    $result["id"] = $return;
			  
			} else {
			   $result['code'] = 0;
			   $error = $this->mimoment_tag_db->getError();
			   $result['info'] = $error?$error:"发生未知错误";
			}		
			Output::instance()->object($result);
		}	
		
		
	//删除标签接口
		public	function delTag($id){
			
			  $result = array();		
				$where['id']=$id;
				$res=$this->mimoment_tag_db->where($where)->delete();
				//log::write('sql:'.$userAccessTagModel->getLastSql());
				if ($res) {
					$result['code'] = 1;
			     
				} else {
					$result['code'] = 0;
					 $error = $this->mimoment_tag_db->getError();
					 $result['info'] = $error?$error:"发生未知错误";
				}
				Output::instance()->object($result);
		}
}